package com.gxa.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.base.RespResult;
import com.gxa.order.dto.OrderTestDto;
import com.gxa.order.dto.ServiceCommentsDTO;
import com.gxa.order.dto.ServiceUserProofDTO;
import com.gxa.order.entity.ServiceComments;
import com.gxa.order.entity.ServiceCommentsImage;
import com.gxa.order.entity.ServiceOrder;
import com.gxa.order.entity.ServiceUserProof;
import com.gxa.order.mapper.*;
import com.gxa.order.mapper.ServiceOrderAppMapper;
import com.gxa.order.mapper.ServiceOrderMapper;
import com.gxa.order.service.ServiceOrderAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ServiceOrderAppServiceImpl extends ServiceImpl<ServiceOrderAppMapper, ServiceOrder> implements ServiceOrderAppService {
    @Autowired
    private ServiceUserProofMapper serviceUserProofMapper;
    @Autowired
    private ServiceOrderAppMapper serviceOrderAppMapper;
    @Autowired
    private ServiceCommentsMapper serviceCommentsMapper;
    @Autowired
    private ServiceCommentsImageMapper serviceCommentsImageMapper;

    /**
     * 上传图片业务
     * @param multipartFile
     * @param request
     * @return
     * @throws IOException
     */
    @Override
    public RespResult upload(MultipartFile[] multipartFile, HttpServletRequest request) throws IOException {
        Boolean flage=false;
        int length = multipartFile.length;
        String[] file=new String[length];
        for (int i = 0; i < multipartFile.length; i++) {
            String filename = multipartFile[i].getOriginalFilename();
            String filenum = UUID.randomUUID().toString();
            String load="ocean-order/src/main/resources/img";
            if (!new File(load).exists()){
                new File(load).mkdir();
            }
            String realfile=load+"/"+filenum+"_"+filename;
            File file1 = new File(realfile);
            InputStream inputStream = multipartFile[i].getInputStream();
            FileOutputStream outputStream = new FileOutputStream(file1);
            byte[] bytes = new byte[1024];
            int b=-1;

            while ((b=inputStream.read(bytes))!=-1){
                outputStream.write(bytes);
                flage=true;
            }
           file[i]=realfile;
        }
        if (flage){
            return RespResult.success(file);
        }
        return RespResult.error("上传失败");
    }

    /**
     * 上传用户凭证
     * @param userProofDTO
     * @return
     */
    @Override
    @Transactional
    public RespResult uploadProof(ServiceUserProofDTO userProofDTO) {
        ServiceUserProof proof = new ServiceUserProof();
        proof.setImgurl(userProofDTO.getImgurl());
        proof.setPayType(userProofDTO.getPayType());
        proof.setRemarks(userProofDTO.getRemarks());
        int insert = serviceUserProofMapper.insert(proof);
        System.out.println(userProofDTO.getOrderId());
        Boolean flag = serviceOrderAppMapper.updateOrderByUserProof(userProofDTO.getOrderId(), proof.getId(), new Date());
        if (flag){
            return RespResult.success();
        }
        return RespResult.error("新增失败");
    }

    /**
     * 订单评论
     * @param commentsDTO
     * @return
     */
    @Override
    public RespResult comment( ServiceCommentsDTO commentsDTO) {
        ServiceComments serviceComments = BeanUtil.copyProperties(commentsDTO, ServiceComments.class);
        serviceComments.setCommentsDatetime(new Date());
       serviceCommentsMapper.insert(serviceComments);
        serviceOrderAppMapper.updateCommentId(commentsDTO.getOrderId(),serviceComments.getServiceCommentsId(),new Date());
        String[] imgurl = commentsDTO.getImgurl();
        boolean flag=false;
        for (int i = 0; i <imgurl.length; i++) {
            ServiceCommentsImage
                    serviceCommentsImage = new ServiceCommentsImage();
            serviceCommentsImage.setScImageUrl(imgurl[i]);
           serviceCommentsImage.setServiceCommentsId(serviceComments.getServiceCommentsId());
            int insert = serviceCommentsImageMapper.insert(serviceCommentsImage);
            flag=true;
        }
        if (flag){
            return RespResult.success();
        }else {
            return RespResult.error("上传失败");
        }

    }


    @Override
    public RespResult orderAll() {
        List<OrderTestDto> orderTestDtos = serviceOrderAppMapper.selectAllOrder();
        return RespResult.success(orderTestDtos);
    }
}
